package com.rzhd.coursepatriarch.common.view.dialog;

import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import com.rzhd.coursepatriarch.common.R;

/**
 * @author yql
 * Time: 2018/4/25.
 */

public class LoadingDialog extends DialogFragment {

    private final String TAG = getClass().getSimpleName();
    private OnDialogCancelListener mDialogCancelListener;

    /**
     * 设置对话框的风格和各种属性，但是千万别设置关于view的东西，因为这时候对话框还没建立呢
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //dialog弹出后会点击屏幕或物理返回键，dialog不消失
//        setCancelable(false);

        //可以设置dialog的显示风格
        setStyle(STYLE_NO_TITLE, 0);
    }

    /**
     * 千万别同时使用onCreatView和onCreatDialog方法，他们仅仅是为了完成同样一个目的的两条路而已。
     * 从生命周期的顺序而言，先执行onCreateDialog()，后执行onCreateView()
     * @param savedInstanceState
     * @return
     */
    /*@NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }*/

    /**
     * 有关于view的东西在onCreatView中去设置
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_loading_progress, container, false);
        intiView(view);
        return view;
    }

    /**
     * 界面初始化后设置关于屏幕的
     */
    @Override
    public void onStart() {
        super.onStart();
        //dialog弹出后会点击屏幕，dialog不消失；点击物理返回键dialog消失
        getDialog().setCanceledOnTouchOutside(false);
        Window window = getDialog().getWindow();
        //两种方式实现修改透明度
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        //透明度1.0不透明
//        layoutParams.dimAmount = 0.0f;
        window.setAttributes(layoutParams);
        //不显示状态栏
//        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        //消除锯齿
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    /**
     * 初始化界面
     *
     * @param contentView view对象
     */
    private void intiView(View contentView) {
        ImageView imageView = contentView.findViewById(R.id.progressbar);
        Animation animation = new RotateAnimation(0.0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setInterpolator(new LinearInterpolator());
        //持续时间
        animation.setDuration(2 * 1000);
        //重复次数（一直）
        animation.setRepeatCount(-1);
        imageView.startAnimation(animation);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        if (mDialogCancelListener != null) {
            mDialogCancelListener.onCancel();
        }
    }

    /**
     * 一个tag（String）通过这个tag可以告诉fragment是谁启动了它
     *
     * @param manager 管理类
     */
    public void show(FragmentManager manager) {
        show(manager, getClass().getSimpleName());
    }

    public void setDialogCancelListener(OnDialogCancelListener dialogCancelListener) {
        mDialogCancelListener = dialogCancelListener;
    }

    public interface OnDialogCancelListener {
        void onCancel();
    }
}
